<?php
class MproductsAction extends PublicAction {
	//会员发布商品列表页
	public function index(){
		$uid = session('uid');
		$this->checkuid($uid,U('Mproducts/index'));
		$productsList = D('Products')->getMproductsList(array('p.uid'=>$uid,'p.products_close'=>0),'p.products_id desc');
		//var_dump($productsList);die;
		$this->msg = I('msg',0);
		$this->uid = $uid;
		$this->productsList = $productsList;
		$this->display();
	}
	
	//会员发布商品详情页
	public function detail(){
		$uid = session('uid');
		$pid = I('pid');
		$this->checkuid($uid,U('Mproducts/detail',array('pid'=>$pid)));
		
		if(!$pid){
			$this->redirect('index',array('uid'=>$uid));
		}
		$categories = M('products_to_categories')->where(array('products_id' => $pid))->find();
	
		$infoList = D('ProductsView')->getOne(array('p.products_id' => $pid));
		/* echo "<pre>";
		var_dump($infoList);die; */
		$BigImg = M('products_images')->where(array('products_id' => $pid))->order('sort_order')->select();
	
		//获取产品分类
		$category = D('Category')->getCateList(array('cd.language_id'=>L('lan_id')),'c.sort_order asc');
		
		$where['fee_id'] = array('in','1,2');
		$where['fee_status'] =1;
		$fee = M('fee')->where($where)->field('fee_id,fee_name,active_day,fee_money')->select();
		//var_dump($fee);die;
		$this->fee = $fee;
		$this->uid = $uid;
		$this->category = $category;
		$this->BigImg  =$BigImg;
		$this->categories = $categories;
		$this->infoList = $infoList;
		$this->display();
	
	}
	
	//发布商品
	public function publish(){
		$uid = session('uid');		
		$this->checkuid($uid,U('Mproducts/publish'));
		$citycode = $citycode?$citycode:session('citycode');//获取默认城市参数
		$nowCityProvince = M('city')->where(array('code'=>$citycode))->field('code,provincecode')->find();
		
		$member = M('membership')->where(array('uid'=>$uid))->field('name,nickname,tel,province,city')->find();
		if(!$member['tel']){//未绑定手机进行手机绑定
			$this->redirect('Member/clocktel',array('uid'=>$uid));
		}
		//获取产品分类
		$category = D('Category')->getCateList(array('cd.language_id'=>L('lan_id')),'c.sort_order asc');
		$this->category = $category;
		/*  echo "<pre>";
			var_dump($category);;die;  */
		$where['fee_id'] = array('in','1,2');
		$where['fee_status'] =1;
		$fee = M('fee')->where($where)->field('fee_id,fee_name,active_day,fee_money')->select();
		//var_dump($fee);die;
		$this->fee = $fee;
		$this->nowCityProvince = $nowCityProvince;
		$this->member = $member;
		$this->uid = $uid;
		$this->display();
	}
	//处理发布商品或修改商品
	public function dpublish(){
		if(IS_POST){
			$uid = I('uid');
			$this->checkuid($uid,U('Mproducts/publish'));
			$data['products_name'] = I('post.products_name');
			$data['products_description'] = I('post.products_description');
			$data['products_oldprice'] = I('post.products_oldprice');
			$data['products_type'] = I('post.products_type');
			$data['products_paimai_price'] = I('post.products_paimai_price');
			$data['products_date_avaliable'] = I('post.products_date_avaliable');
			$data['provcode'] = I('post.provcode');
			$data['citycode'] = I('post.citycode');
			$data['area_id'] = I('post.area_id');
			$data['contact_person'] = I('post.contact_person');
			$data['contact_tel'] = I('post.contact_tel');
			$data['contact_qq'] = I('post.contact_qq');
			$data['contact_wechat'] = I('post.contact_wechat');
			$data['contact_address'] = I('post.contact_address');
			$up_flag = I('post.up_flag');
			$jiaji_flag = I('post.jiaji_flag');
			$data['uid'] = I('post.uid');
	
			$data['products_quantity'] = I('post.products_quantity');
			$data['products_model'] = I('post.products_model');
			$data['products_price'] = I('post.products_price');
			$data['products_date_added'] =  date("Y-m-d H:i:s");
			$data['products_weight'] = I('post.products_weight');
			$data['products_status'] = 1;
			//$data['sort_order'] = I('post.sort_order');
			$files = I('post.file');
			/*  echo "<pre>";
				var_dump($files);die; */
			$data['products_image'] = $files[0];
			//var_dump($data);die;
			$products = M("products");
			$change = I('change','');
			
			if($change){//修改商品
				
				$data['products_last_modified'] =  date("Y-m-d H:i:s");
				$pid = I('products_id');
				//echo $pid;die;
				$oproducts = M('products')->where(array('products_id'=>$pid))->field('up_flag,jiaji_flag')->find();
				
				$data['products_id'] = $pid;
				$products->save($data);
				//分类处理
				M('products_to_categories')->where(array('products_id' => $pid))->delete();
				$cateArr = I('post.categories_id');	
				$cateMap['products_id']  = $pid;
				$cateMap['categories_id'] = $cateArr;
				//var_dump($cateMap);die;
				M('products_to_categories')->add($cateMap);
				/* var_dump($cateArr);die;
				foreach($cateArr as $v){
					$cateMap['products_id']  = $pid;	 
					$cateMap['categories_id'] = $v;
					M('products_to_categories')->add($cateMap);
				} */
				
				M('products_images')->where(array('products_id'=>$pid))->delete();
				foreach($files as $k => $v){
					$picMap['products_id'] = $pid;
					$picMap['image'] = $v;
					M("products_images")->add($picMap);
				}
				
				//处理置顶或者加急收费
				if($up_flag&&$jiaji_flag){
					if(!$oproducts['up_flag']&&!$oproducts['jiaji_flag']){//原本两个都没有选
						$this->redirect('Order/fee',array('pid'=>$pid,'feeType'=>'upjj'));
					}elseif(!$oproducts['up_flag']){//置顶没选
						$this->redirect('Order/fee',array('pid'=>$pid,'feeType'=>'up'));
					}elseif(!$oproducts['jiaji_flag']){//加急没选
						$this->redirect('Order/fee',array('pid'=>$pid,'feeType'=>'jiaji'));
					}
					
				}elseif($up_flag&&!$oproducts['up_flag']){//原本没有选up_flag
					$this->redirect('Order/fee',array('pid'=>$pid,'feeType'=>'up'));
				}elseif($jiaji_flag&&!$oproducts['jiaji_flag']){//原本没有选jiajia_flag
					$this->redirect('Order/fee',array('pid'=>$pid,'feeType'=>'jiaji'));
				}
				$this->redirect('index',array('uid'=>$uid,'msg'=>'商品修改成功!'));
			}else{//发布商品				
				$insertId = $products->add($data);
				$products->setField('sort_order', $insertId);
				$cateArr = I('post.categories_id');
				$map['products_id']  = $insertId;
				$map['categories_id'] = $cateArr;
				M('products_to_categories')->add($map);
				/* foreach($cateArr as $v){
					$map['products_id']  = $insertId;
					$map['categories_id'] = $v;
					M('products_to_categories')->add($map);
				} */
				
				foreach($files as $k => $v){
					$picMap['products_id'] = $insertId;
					$picMap['image'] = $v;
					$picMap['sort_order'] = $k;
					M("products_images")->add($picMap);
				}
				
				//处理置顶或者加急收费
				if($up_flag&&$jiaji_flag){
					$this->redirect('Order/fee',array('pid'=>$insertId,'feeType'=>'upjj'));
				}elseif($up_flag){
					$this->redirect('Order/fee',array('pid'=>$insertId,'feeType'=>'up'));
				}elseif($jiaji_flag){
					$this->redirect('Order/fee',array('pid'=>$insertId,'feeType'=>'jiaji'));
				}	
				$this->redirect('index',array('uid'=>$uid,'msg'=>'发布商品成功!'));
			}
			
			
				
		}else{
			$this->redirect('index',array('uid'=>$uid));
		}
	}
	

	/*======================================================================
	 刪除批量上傳的圖片
	 ======================================================================================*/
	public function removeImg(){
		$file = $_GET['file'];
		@unlink("./Public/images/products/".$file);
		@unlink("./Public/images/products/thumb/m_".$file);
		@unlink("./Public/images/products/thumb/s_".$file);
	}
	/*======================================================================
	 刪除批量已保存到數據庫的圖片和數據
	 ======================================================================================*/
	public function removePic(){
		$id = $_GET['id'];
		$file = M('products_images')->where(array('id'=> $id))->getField('image');
		@unlink("./Public/images/products/".$file);
		@unlink("./Public/images/products/thumb/m_".$file);
		@unlink("./Public/images/products/thumb/s_".$file);
		M('products_images')->where(array('id'=> $id))->delete();
	}
	
	//删除商品
	public function delete(){
		$uid = session('uid');
		$pid = I('pid');
		$this->checkuid($uid,U('Mproducts/delete',array('pid'=>$pid)));
		//$pid = I('pid');
		$data['products_id']=$pid;
		$data['products_close']=1;
		$data['close_reason']= I('reason');
		$result = M('Products')->save($data);//where(array('products_id'=>$pid))->setField('products_close', 1);
		if($result){
			$this->redirect('index',array('uid'=>$uid,'msg'=>'关闭商品成功！'));
		}else{
			$this->redirect('index',array('uid'=>$uid,'msg'=>'关闭商品失败，请联系管理人员！'));
		}
		
		
	}
}